package com.itheima.putige.putige.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.putige.putige.common.R;
import com.itheima.putige.putige.entity.Category;
import com.itheima.putige.putige.service.CategoryService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/*
* 分类管理的业务处理
* */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /*
    * 添加分类
    * */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("添加成功！");
    }

   /*
   * 获取分类，用于后台和移动端展示
   * */
    @GetMapping("/page")
    public R<Page> get(int page,int pageSize){
        Page objectPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Category::getSort);
        categoryService.page(objectPage,queryWrapper);
        return R.success(objectPage);
    }

    /*
    * 删除分类
    * */
    @DeleteMapping
    public R<String> remove(Long id){
        categoryService.remove(id);
        return R.success("删除成功！");
    }

    /*
    * 修改分类
    * */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改成功!");
    }

    @GetMapping("/list")
    public R<List<Category>> list(Integer type){
        List<Category> list = categoryService.list(new LambdaQueryWrapper<Category>().eq(type != null,Category::getType,type));
        return R.success(list);
    }
}
